#include "stdafx.h"
#include "Test.h"

#include <dbt.h>
#include <Shellapi.h>
#include <Shlwapi.h>
//#include <Strsafe.h>

#include "Foxdev.h"
#include "Dialog.h"

#pragma comment(lib,"Shlwapi.lib")

#define	WM_NOTIFYICON	(WM_USER+1)
#define	WM_UPGRADE		(WM_USER+2)

class Test:public Dialog{
public:
	static void	run(HINSTANCE inst);
	static TCHAR	appName[];
	static TCHAR	nosetup[];
	static TCHAR	upgrade[];
	static TCHAR	noupgrade[];

private:
	static void	uniqueness	(void);

	Test(HINSTANCE inst):Dialog(inst,MAKEINTRESOURCE(IDD_ABOUTBOX),0){};

	LRESULT onCommand		(WPARAM,LPARAM);
	LRESULT onDeviceChange	(WPARAM,LPARAM);
};

TCHAR	Test::appName[]=L"TEST";

LRESULT Test::onCommand(WPARAM wparam,LPARAM lparam){
	switch(LOWORD(wparam)){
	case IDOK:
	case IDCANCEL:
		PostQuitMessage(0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
LRESULT Test::onDeviceChange(WPARAM wparam,LPARAM lparam){
	switch(wparam){
	case DBT_DEVICEREMOVECOMPLETE:
		Foxdev::closeAll();
		break;
	case DBT_DEVNODES_CHANGED:
		Foxdev::closeAll();
	case DBT_DEVICEARRIVAL:
		Foxdev::openAll();
		break;
	default:
		return 1;
	}
	return 0;
}
void Test::run(HINSTANCE instance){
	uniqueness();
	HICON icon=LoadIcon(instance,MAKEINTRESOURCE(IDI_FOXYNC));
	setCursor(LoadCursor(0,IDC_ARROW));
	//menu=LoadMenu(instance,MAKEINTRESOURCE(IDC_FOXYNC));
	Test *x=new Test(instance);
	Foxdev::openAll();
	x->show();
	x->Window::messageLoop();
	Foxdev::closeAll();
	delete x;
}
void Test::uniqueness(void){
	HANDLE mutex=CreateMutex(NULL,FALSE,appName);
	if(mutex!=NULL){
		if(GetLastError()!=ERROR_ALREADY_EXISTS)
			return;
	}
	ExitProcess(0);
}
int APIENTRY _tWinMain(HINSTANCE instance,HINSTANCE prevInstance,
	LPTSTR cmdLine,int cmdShow){
	Test::run(instance);
	return 0;
}